Exercise 34 練習34 <<
Previous Next >> Exercise 28 練習28
Exercise 24 練習24
Draw A Game Board

畫遊戲板
This exercise is Part 1 of 4 of the Tic Tac Toe exercise series. The other exercises are: Part 2, Part 3, and Part 4.
Time for some fake graphics! Let’s say we want to draw game boards that look like this:
此練習是Tic Tac Toe練習系列4的第1部分。其他練習是:第2部分,第3部分和第4部分。
是時候購買一些假圖形了!假設我們要繪製如下游戲板:
--- --- ---
| | | |
--- --- ---
| | | |
--- --- ---
| | | |
--- --- ---
This one is 3x3 (like in tic tac toe). Obviously, they come in many other sizes (8x8 for chess, 19x19 for Go, and many more).
Ask the user what size game board they want to draw, and draw it for them to the screen using Python’s print
statement.
Remember that in Python 3, printing to the screen is accomplished by
print("Thing to show on screen")
Hint: this requires some use of functions, as were discussed previously on this blog and elsewhere on the Internet, like this TutorialsPoint link.
這是3x3(就像井字遊戲一樣)。顯然,它們有許多其他尺寸(國際象棋為8x8,圍棋為19x19等)。
詢問用戶他們要繪製什麼尺寸的遊戲板,並使用Python的print
語句將其繪製到屏幕上。
請記住,在Python 3中,打印到屏幕是通過以下方式完成的:
print("Thing to show on screen")
提示:這需要某種功能的使用,如本博客之前和Internet上其他地方所討論的,例如TutorialsPoint鏈接。
Topics and links for more information
主題和鏈接,以獲取更多信息
The main topic of this exercise is functions. They are tricky, and deserve lots of practice and thought. Here are a few links in case you want some more reading.
- Functions on TutorialsPoint
- Hands-on Python, by Loyola University
- Learn Python the Hard Way
- ZetCode
There are hundreds more out there - read and practice away!
本練習的主要主題是功能。它們很棘手,值得大量實踐和思考。這裡有一些鏈接,以備您需要更多閱讀。
- TutorialsPoint上的功能
- Loyola University的動手Python
- 艱苦學習Python
- 郵遞區號
那裡還有數百個-閱讀和練習吧!
Solutions 解決方案
Sample solution
Let’s break this task into pieces and use Python 3.
First, the user needs to a numbers that represent the size of the game board (assuming the game board will be n by n). (Bonus exercise: extend the following code to make the game board not square.). The following snippet assumes that the person entered a number and doesn’t do any kind of error checking - for now, that’s OK.
board_size = int(input("What size of game board? "))
Then, we need to draw each row of the game board. Each row consists of horizontal pieces (---
) and vertical pieces (|
). Each of these shows up in a pattern, so we can rely on for loops to help with the rendering.
To print a single row, we want to do something like this:
print(" --- " * board_size)
To print the vertical parts of the row, we want something like this, because we don’t care about trailing whitespace, and because we want one more vertical line than the size of the board:
print("| " * (board_size + 1))
For a board of size board_size we want to print that many horizontal pieces and vertical pieces, plus an extra horizontal piece for the bottom. Let’s use functions for this entire operation, since we might want to change the style on the game boards for later use. All together, the program will look like this:
def print_horiz_line():
print(" --- " * board_size)
def print_vert_line():
print("| " * (board_size + 1))
if __name__ == "__main__":
board_size = int(input("What size of game board? "))
for index in range(board_size):
print_horiz_line()
print_vert_line()
print horiz_line()
This way, if we ever decide to change the design of the game board by making it bigger, it will be easy to do! All we need to do is change the print_horiz_line()
and print_vert_line()
functions, and we’re all set!
範例解答
讓我們將這項任務分解成幾個部分並使用Python 3。
首先,用戶需要一個代表遊戲板尺寸的數字(假設遊戲板為n×n)。(額外的練習:擴展以下代碼以使遊戲板不方形。)。以下代碼段假定此人輸入了一個數字,並且沒有進行任何類型的錯誤檢查-暫時可以。
board_size = int(input("What size of game board? "))
然後,我們需要繪製遊戲板的每一行。每行由水平部分(---
)和垂直部分(|
)組成。每一個都以一種模式顯示,因此我們可以依靠for循環來幫助進行渲染。
要打印一行,我們想做這樣的事情:
print(" --- " * board_size)
要打印行的垂直部分,我們需要這樣的東西,因為我們不在乎尾隨空格,並且因為我們想要的垂直線比板子的尺寸還要多:
print("| " * (board_size + 1))
對於大小為board_size的板子,我們要打印許多水平件和垂直件,並在底部再增加一個水平件。讓我們在整個操作過程中使用函數,因為我們可能想更改遊戲板上的樣式以備後用。總之,該程序將如下所示:
def print_horiz_line():
print(" --- " * board_size)
def print_vert_line():
print("| " * (board_size + 1))
if __name__ == "__main__":
board_size = int(input("What size of game board? "))
for index in range(board_size):
print_horiz_line()
print_vert_line()
print horiz_line()
這樣,如果我們決定通過擴大遊戲板的設計來改變它的設計,那將很容易做到!我們需要做的就是更改print_horiz_line()
和print_vert_line()
功能,並且一切就緒!
A few user solutions not using functions
一些不使用功能的用戶解決方案
This one is pretty simple, and it is specific to the 3x3 board above. There is no way to change the size. Additionally, writing out the a
and b
lists in the print statement are tedious and can be improved with a list comprehension or for loop.
這很簡單,它特定於上面的3x3板。無法更改大小。此外,在打印語句中寫出a
andb
列表很繁瑣,可以通過列表理解或for循環加以改進。
a = '---'.join(' ')
b = ' '.join('||||')
print('\n'.join((a, b, a, b, a, b, a)))
The author chose to use a while
loop instead of a for
loop to do the printing, which
was an interesting choice. But it works!
作者選擇使用while
循環而不是for
循環進行打印,這是一個有趣的選擇。但這有效!
def drawboard(kamal):
kamal = int(kamal)
i = 0
ho = "--- "
ve = "| "
ho = ho * kamal
ve = ve * (kamal+1)
while i < kamal+1:
print ho
if not (i == kamal):
print ve
i += 1
Exercise 34 練習34 <<
Previous Next >> Exercise 28 練習28